<template>
    <el-select v-model="sval" :class="className" :popper-class="popperClass" clearable placeholder="请选择" :style="{width}"  @focus="onFocus" @change="onChcange">
        <el-option v-for="dt in lists" :key="dt.stageTaskXuntianId" :value="dt.stageTaskXuntianId" :label="dt.xuntianName" />
    </el-select>
</template>
<script setup>
import {ref,onMounted,watch,nextTick} from "vue"
import {getXuntianList} from '@/api/plant'

const props = defineProps({
    modelValue:'',
    width:{
        type:String,
        default:'100%'
    },
    params:{  // 查询条件
        type:Object,
        default:{}
    }, 
    className:'',
    popperClass:"",
    optLists:'',
})
const emit = defineEmits(['update:modelValue','change','focus'])
const sval = ref('')
const lists = ref('')

onMounted(()=>{ 
    //getTableData()
    if(props.optLists){
        lists.value = props.optLists
        sval.value = props.modelValue || '' // 设置默认选项
    }else{
        getTableData()
    } 
})

watch(()=>props.modelValue,(n,o)=>{
    sval.value = n
})
watch(()=>props.optLists,(n,o)=>{
    lists.value = n
})

const getTableData = async (params) => {
    const pm = params?params:props.params
  const res = await getXuntianList({ page: 1, limit: 1000000,...pm})
  if (res.code == 0) {
    lists.value = res.data
    //console.log("GGGGG:",props)
    sval.value = props.modelValue || '' // 设置默认选项
  }
}

const onChcange = ()=>{
    emit('update:modelValue',sval.value) 
    //emit('change',sval.value) 
    if(sval.value){
        emit('change',lists.value.find(ele =>ele.farmId == sval.value))
    }else{
        emit('change',{}) 
    }
}

const onFocus = () => {
    emit('focus', {})
}

defineExpose({getTableData})

</script>

<style scoped lang="scss">
</style>